/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.c1202m.connection;

import com.c1202m.gui.CustomMessageDialog;
import java.io.File;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * Hibernate Utility class with a convenient method to get Session Factory object.
 *
 * @author CodeBlue
 */
public class HibernateUtil {

    private static final SessionFactory sessionFactory;
    
    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new Configuration().configure(new File("cfg.xml")).buildSessionFactory();
        } catch (Throwable ex) {
            new CustomMessageDialog(null, true, "Config file not found!", "Don't try to delete config file. Please re-run program..", CustomMessageDialog.MESSAGE);
            ex.printStackTrace();
            System.exit(0);
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}
